function gotoadd(dom){
	var dialog = editAdminDataDialog("left,top",dom,label_add,"/adminconsole/Console/Corporate/AddCorporate.jsp","addDialog",AddEventClickuserCorporate,loadTaxElementFinish,null);
}

function AddEventClickuserCorporate(dialog,id){
	var title = dialog.find("#title").val();
	var country = dialog.find("#country").val();
	var state = dialog.find("#state").val();
	var phone = dialog.find("#phone").val();
	var email = dialog.find("#email").val();
	var url = "/agencyService";
	if(id == null){
		var func = "addcorporate";
	}else{
		var func = "editcorporate"
	}
	var data = { func : func,
			id : id,
			title : title,
			country : country,
			state : state,
			phone : phone,
			email : email,
	}
	simpleserviceWithArgs(url,data,showUserCorporate,"add");
}

function loadTaxElementFinish(dialog){
	dialog.parents(".ui-dialog:first").css("width",600);
	print_countryCorporate("country");
	var t1 = $("[id*=country]:visible")[0].selectedIndex;
	print_stateCorporate("state",t1);
	//editcompany();
}

function EdituserCorporate(dom,id){
	var dialog = editAdminDataDialog("right",dom,label_add,"/adminconsole/Console/Corporate/AddCorporate.jsp","addDialog",AddEventClickuserCorporate,loadEditElementFinish,id);
}

function loadElementFinish(dialog){
	dialog.parents(".ui-dialog:first").css("width",600);
}

function loadEditElementFinish(dialog,id){
	cursorDialogResize(dialog,{width:600});
	print_countryCorporate("country");
	EdituserCorporateset(id,dialog);
}

function setBar(){
	var title =	$(".breadcrumb").html().trim();
	$(".breadcrumb").replaceWith("<div class='breadcrumb'>" +
			"<a href='javascript:showAgent("+'undefined'+","+'undefined'+",0)'><span class='link' id='titleLinkPOSCategory'>"+title+"</span></a>/"+
			'<span id="titleName" >'+console_registerAgent+"</span></div>");
	$(".destitle").replaceWith('<div class="destitle">'+console_suggestUser+'</div>');
	$(".titlelist").replaceWith('<div class="titlelist">'+console_listUser+'<span id="listsize">'+$("#listsize").html()+'</span>'+'</div>');
	$("[name=add_agent]").val(console_btnAddUserAgent);
	$("[name=add_agent]").attr('onclick','alert()');
	$("[name=del_agent]").val(console_btnDeleteUserAgent);
	$("[name=del_agent]").attr('onclick','alert()');
}

function showUserAgent(id,type){
	setBar();
	if(id == null||type=="add"||type=="del"){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
	}else if(id == "next"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())+1;
	}else if(id == "previous"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())-1;		
	}
	var link = "/agencyService";
	var data = { func: "getCorporate",
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
					}
	simpleservice(link,data,showUserCorporateList);
}

function showUserCorporateList(data){
	$("[id=showList]").load("/adminconsole/Console/Agent/userAgent.jsp",function(responseText, textStatus, XMLHttpRequest) {
		objArrayFirst = JSON.parse(data);
		var sumtext = "";
		var text = "";
		var pageAmount = Math.ceil(objArrayFirst.dataAmount/objArrayFirst.lineAmount);
		var line = objArrayFirst.lineAmount;
		var pagePointer = objArrayFirst.pageNumber;
		var objArray = objArrayFirst.data;
		$("#listsize").html("("+objArrayFirst.dataAmount+")");
		var lineNumber = (line * pagePointer) - (line-1);
		var Percent = "";
		var description = "";
		for(var i = 0;i< objArray.length+1;i++){
				if(objArray[i]!=undefined){
					var organization = "";
					var country = "";
					var phone = "";
					var email = "";
					if(objArray[i].organization!=null)
						organization = objArray[i].organization;
					if(objArray[i].country!=null)
						country = objArray[i].country;
					if(objArray[i].phone!=null)
						phone = objArray[i].phone;
					if(objArray[i].email!=null)
						email = objArray[i].email;
				}
				if(i != objArray.length ){
					text = "<tr id=\"row_"+objArray[i]['id']+"\" style=\"border: 0px;\"> " +
							"<td id=\"col_1\" align=\"center\"> "+(lineNumber+i)+". </td>"+
							"<td><input type=\"checkbox\" id=\"id_"+i+"\" name=\"ids[]\" value=\""+objArray[i]['id']+"\"> </td>"+
							"<td align=\"left\"> "+objArray[i]['displayId']+" </td>"+
							"<td align=\"left\" class='link' >"+organization+" </td>"+
							"<td>"+country+"</td>"+
							"<td>"+phone+"</td>"+
							"<td>"+email+"</td>"+
							"<td></td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"EdituserCorporate($(this),"+objArray[i]['id']+")\">Edit</a></td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"changeStatusUserCorporate("+objArray[i]['id']+") \"> " +
							"<span id =status"+objArray[i]['id']+">"+objArray[i]['status']+"</a>" +
							"</td>" +
							"</tr>";
					sumtext += text;
				}else{
					text = "<tr>"+
					"<td colspan=\"13\" style=\"padding:0;\">"+
					"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
					"<tbody>"+
						"<tr>"+
							"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
								"<select name=\"selectpage\" onchange=\"showPOSItem()\">"+
								"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
							"</td>"+
							"<td width=\"31%\" align=\"center\">"+foor_view+
								"<select name=\"maxEntries\" onchange=\"showPOSItem()\">"+
									"<option style=\"color:#000000\"  value=\"10\">10</option>"+
									"<option style=\"color:#000000\"  value=\"20\">20</option>"+
									"<option style=\"color:#000000\"  value=\"50\">50</option>"+
									"<option style=\"color:#000000\"  value=\"100\">100</option>"+
									"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
								"</select>"+foor_rpp+" </td>" +
								"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
								" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
								"</tbody></table></td></tr>";
					sumtext += text;
				}
		}
		$("#showList tr").not($("#showList tr").first()).remove();
		$("#showList").append(sumtext);
		$("#pageAmount").text(pageAmount);
		if(pagePointer+1 <= pageAmount){
			$("#next").attr("onclick","showUserCorporate(id)");
			$("#next").html("<a href=\"#\">Next&gt;&gt;</a>");
		}
		if(pagePointer > 1){
			$("#previous").attr("onclick","showUserCorporate(id)");
			$("#previous").html("<a href=\"#\">&lt;&lt;Previous</a>");
		}
		$("[name=maxEntries]").val(line);
		for ( var i = 0; i < pageAmount; i++) {
			$("[name=selectpage]").append(
					$("<option>").val((i+1)).html((i+1)));
			}
		$("[name=selectpage]").val(pagePointer);
	});
	closeWaitingDialog();
}

function EdituserCorporateset(id,dialog){
	if(id != null){
		var url = "/agencyService";
		var data = { func : "loadagency",
				agencyId : id
			}
		simpleserviceWithArgs(url,data,editSuccessUserCorporate,dialog);
	}
}

function editSuccessUserCorporate(data,dialog){
	var objArray = new Array();
	objArray = JSON.parse(data);
	dialog.find("#title").val(objArray['organization']);
	dialog.find("#country option[value='"+objArray['country']+"']").attr('selected','selected');
	var t1 = $("[id*=country]:visible")[0].selectedIndex;
	print_stateCorporate("state",t1);
	dialog.find("#state option[value='"+objArray['state']+"']").attr('selected','selected');
	dialog.find("#phone").val(objArray['phone']);
	dialog.find("#email").val(objArray['email']);
}

function changeStatusUserCorporate(id){
	waitingDialog({});
	var url = "/agencyService";
	var data = { func : "editStatusCorporateById",
			id : id
		}
	simpleservice(url,data,changeStatusSuccessUserCorp);
}

function changeStatusSuccessUserCorp(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	$("#status"+objArray["id"]).text(objArray["status"]);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function deleteUserCorporate(){
	waitingDialog({});
	var listCheckBox = "";
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){	
			listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	var url = "/agencyService";
	var data = { func : "delcorporate",
			agencyId : listCheckBox.substring(0,listCheckBox.length - 1)
		}
	simpleserviceWithArgs(url,data,showUserCorporate,"del");
}

function toggleSelect(){
	if($("#showList tr [type=checkbox]")[0].checked == true){
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = true;
		}
	}else{
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = false;
		}
	}
}